<template>
  <div id="tab-bar-item" @click="itemClick">
    <div v-show="!isActive" class="item-icon">
      <slot name="icon"></slot>
    </div>
    <div v-show="isActive" class="item-icon-active">
      <slot name="icon-active"></slot>
    </div>
    <div class="item-title" :style="activeColor">
      <slot name="title"></slot>
    </div>
  </div>
</template>

<script>
export default {
  name: "TabBarItem",
  props: {
    link: {
      type: String,
      required: true,
    }  ,
    textColor: {
      type: String,
      default: "#000",
    }, 
  },
  computed: {
    isActive() {
      return this.$route.path.indexOf(this.link) === 0;
    },
    activeColor() {
      return this.isActive ? { color: this.textColor } : {};
    },
  },
  methods: {
    itemClick() {
      if (this.$route.path !== this.link) {
        this.$router.replace(this.link);
      }
    },
  },
};
</script>

<style scoped>
#tab-bar-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  align-items: center;
  font-size: 12px;
}
.item-icon,
.item-icon-active,
img {
  width: 24px;
  height: 24px;
}
</style>